public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }

    //买卖股票的最佳时机含手续费
    public int maxProfit(int[] prices, int fee) {
        int m = prices.length;
        int[] f = new int[m];
        int[] g = new int[m];
        f[0] = -prices[0];
        for (int i = 1; i < m; i++) {
            f[i] = Math.max(f[i - 1], g[i - 1] - prices[i]);
            g[i] = Math.max(g[i - 1], f[i - 1] + prices[i] - fee);
        }
        return Math.max(f[m - 1], g[m - 1]);
    }

    //买卖股票的最佳时机 III
    public int maxProfit(int[] prices) {
        int m = prices.length;
        if (m == 0) return 0;
        int[][] f = new int[m][3];
        int[][] g = new int[m][3];
        f[0][0] = -prices[0];
        f[0][1] = f[0][2] = -0x3f3f3f3f;
        g[0][1] = g[0][2] = -0x3f3f3f3f;
        for (int i = 1; i < m; i++) {
            for (int j = 0; j < 3; j++) {
                f[i][j] = Math.max(f[i - 1][j], g[i - 1][j] - prices[i]);
                g[i][j] = g[i - 1][j];
                if (j - 1 >= 0) {
                    g[i][j] = Math.max(g[i - 1][j], f[i - 1][j - 1] + prices[i]);
                }
            }

        }
        return Math.max(Math.max(g[m - 1][0], g[m - 1][1]), g[m - 1][2]);
    }
    
}